package com.atguigu.Util;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.GmallConfig;
import org.apache.phoenix.util.JDBCUtil;
import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.util.List;

/**
 * @author hjy
 * @create 2023/3/21 10:53
 */
public class DimUtil_11 {
    public static JSONObject getDimInfo(Connection connection,String tableName,String id) throws Exception {
        //先从redis查
        Jedis jedis = JedisUtil.getJedis();
        String redisKey="DIM:"+tableName+":"+id;
        String dimInfo = jedis.get(redisKey);
        if (dimInfo!=null){
            jedis.expire(redisKey,24*3600);
            jedis.close();
            return JSONObject.parseObject(dimInfo);
        }

        //从phoenix读取数据
        //拼接sql
        String sql="select * from "+ GmallConfig.HBASE_SCHEMA+"."+tableName+" where id = '"+id+"'";
        List<JSONObject> jdbcUtil = JdbcUtil.getJdbcUtil(connection, sql, JSONObject.class, false);
        JSONObject jsonObject = jdbcUtil.get(0);
        //存入redis
        jedis.setex(redisKey,24*3600,jsonObject.toJSONString());
        jedis.close();
        return jsonObject;

    }
}
